package ru.yermak.bookkeeping.account;

import ru.yermak.bookkeeping.ui.Selector;
import ru.yermak.bookkeeping.model.Account;
import ru.yermak.bookkeeping.common.Command;

import java.awt.event.ActionEvent;
import java.awt.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.context.annotation.Scope;

import javax.swing.*;

/**
 * User: harrier
 * Date: Nov 16, 2008
 */
@Scope("action")
@org.springframework.stereotype.Component
public class AccountDeleteCommand implements Command {

    @Autowired
    @AccountQulifier
    private Selector selector;
    @Autowired
    private AccountDao accountDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void execute(ActionEvent e) {
        Integer id = selector.getSelectedId();
        if (id == null) {
            return;
        }
        Account account = accountDao.getById(id);
        int result = JOptionPane.showConfirmDialog(((Component) e.getSource()), "Are you sure?", "Remove Account", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            accountDao.removeAccount(account);
            selector.selectRow(null);
        }
    }

}
